javaGUI

  javaGUI….笔记
  了解即可。。。

布局

  • 流式布局 FlowLayout 从上到下,从左到右
  • 边界布局 东西南北中
  • 网格布局 网格
  • 网格包布局 大小不确定,但是每一个组件的边界都在网格上
  • 卡片布局 点A出A的页面,点B出B的页面。。。

流式布局

1
2
Frame f=new Frame("hello world");
f.setLayout(new FlowLayout());//给窗体添加流式布局

增加监听器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class FrameDemo {
public static void main(String[] args) {
Frame f=new Frame("hello world.");
f.setLayout(new FlowLayout());
f.setBounds(400, 200, 400, 300);

TextField tf=new TextField(20);
Button bu=new Button("数据转移");
bu.setSize(20,10);
TextArea ta=new TextArea(10,40);

f.add(tf);
f.add(bu);
f.add(ta);

f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

bu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String tf_str=tf.getText();
tf.setText("");
ta.append(tf_str+"\r\n");
//获取光标
tf.requestFocus();
}
});

f.setVisible(true);
}
}

给按钮添加鼠标点击 & 进入 & 离开事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.yifei_02;

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class FrameDemo2 {
public static void main(String[] args) {
Frame f=new Frame("hello world.");
f.setLayout(new FlowLayout());
f.setBounds(400, 200, 400, 300);

Button redButton=new Button("红色");

f.add(redButton);

//设置窗体关闭
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

//对按钮添加动作事件
// redButton.addActionListener(new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// f.setBackground(Color.RED);
// }
// });

//添加鼠标点击事件
redButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
f.setBackground(Color.red);
}
});

//添加鼠标进入事件
redButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
f.setBackground(Color.red);
}

@Override
public void mouseExited(MouseEvent e) {
f.setBackground(Color.white);
}
});

f.setVisible(true);
}
}

键盘监听事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class FrameDemo3 {
public static void main(String[] args) {
Frame f=new Frame("hello world.");
f.setLayout(new FlowLayout());
f.setBounds(400, 200, 400, 300);

Label label=new Label("只能输入数字啊!");
TextField textField=new TextField(40);



f.add(label);
f.add(textField);

//键盘监听事件
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
char ch=e.getKeyChar();
if(!(ch>='0' && ch<='9')){
e.consume();//使此事件不发生
}
}
});

//设置窗体关闭
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}

多级菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

public class FrameDemo4 {
public static void main(String[] args) {
Frame f = new Frame("多级菜单");
f.setLayout(new FlowLayout());
f.setBounds(400, 200, 400, 300);
String name = f.getTitle();
// 菜单栏
MenuBar mb = new MenuBar();
// 菜单
Menu m1 = new Menu("文件");
Menu m2 = new Menu("更改名称");
// 菜单项
MenuItem mi1 = new MenuItem("好好学习");
MenuItem mi2 = new MenuItem("天天向上");
MenuItem mi3 = new MenuItem("恢复标题");
MenuItem mi4 = new MenuItem("打开记事本");
MenuItem mi = new MenuItem("退出系统");

m2.add(mi1);
m2.add(mi2);
m2.add(mi3);
m1.add(m2);
m1.add(mi4);
m1.add(mi);
mb.add(m1);

f.setMenuBar(mb);

mi4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Runtime r = Runtime.getRuntime();
try {
r.exec("notepad");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

mi1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.setTitle(mi1.getLabel());
}
});

mi2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.setTitle(mi2.getLabel());
}
});

mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
f.setTitle(name);
}
});

mi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

// 设置窗体关闭
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}

工具包

public class UiUtil{
    private UiUtil(){}
    //修改窗体的图标
    public static void setFrameImage(JFrame jf){
        //获取工具类对象
        Toolkit tk=Toolkit.getDefaultToolkit();
        //根据路径获取图片
        Image i=tk.getImage("src\\cn\\yifei\\1.jpg");
        //给窗体设置图片
        jf.setIconImage(i);
    }
    //设置窗体居中
    public static void setFrameCenter(JFrame jf){
        Toolkit tk=Toolkit.getDefaultToolkit();
        //获取屏幕宽高
        Dimension d=tk.getScreenSize();
        double srceenWidth=d.getWidth();
        double srceenHeight=d.getHeight();
        //获取窗体宽高
        int frameWidth=jf.getWidth();
        int frameHeght=jf.getHeight();
        //获取新的宽高
        int width=(srceenWidth-frameWidth)/2;
        int height=(srceenHeight-frameHeght)/2;
        //设置窗体坐标
        jf.setLocation(width,height);
    }
}

欢迎与我分享你的看法。
转载请注明出处:http://taowusheng.cn/
微博:寒枫–0-0–
知乎:https://www.zhihu.com/people/tao-wu-sheng
豆瓣:YIFEI